
/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.initilize;

import com.wtkj.exception.BusinessException;
import com.wtkj.vo.dzzf.base.kfwd.Account;
import com.wtkj.vo.dzzf.base.kfwd.CompanyInfo;
import com.wtkj.vo.dzzf.base.kfwd.FundAccount;
import com.wtkj.vo.dzzf.base.kfwd.UserInfo;
import com.wttech.dzzf.process.dao.DataInitilizingDao;
import com.wttech.dzzf.process.dao.TransferUserInfoDao;
import com.wttech.dzzf.process.model.CertKey;
import com.wttech.dzzf.process.model.FundTemp;
import com.wttech.dzzf.process.model.user.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import static com.wttech.dzzf.process.constant.UserConstants.*;

/**
 * 资金账户处理服务实现
 * Created by MaShibo on 2017/10/23.
 */
@Service
public class FundAccountProcessService {

	private static final Logger LOGGER = LoggerFactory.getLogger(FundAccountProcessService.class);


	@Resource
	private AccountProcessService accountProcessService;

	@Resource
	private TransferUserInfoDao transferUserInfoDao;

	@Resource
	private DataInitilizingDao dataInitilizingDao;

	void processFundAccount(FundAccount fundAccount, User user) {
		List<Account> accounts = dataInitilizingDao.queryAccountsByFundAccountId(fundAccount.getFundAccountId());
		accounts.forEach(account -> accountProcessService.processAccount(account, user));
	}

	/**
	 * 选择创建个人或单位客户信息
	 *
	 * @param fundAccount 资金账户
	 * @param user        客户信息
	 */
	public void createUserOrCompanyInfo(FundAccount fundAccount, User user) {
		int userType = user.getUserType();
		//创建客户信息表
		if (userType == DEFAULT_USER_USERTYPE) {
			createUserInfo(fundAccount, user);
		} else {
			createCompanyInfo(fundAccount, user);
		}
	}

	/**
	 * 创建个人客户信息
	 *
	 * @param fundAccount 资金账户信息
	 * @param user        客户信息
	 */
	private void createUserInfo(FundAccount fundAccount, User user) {
		UserInfo userInfo = new UserInfo();
		userInfo.setId(user.getId());
		userInfo.setUserName(user.getUserName());
		userInfo.setUserIdNum(user.getUserIdNum());
		userInfo.setUserIdType(user.getUserIdType());
		userInfo.setRegisteredType(REGISTEREDTYPE_OFFLINE);
		userInfo.setChannelId(user.getChannelId());
		userInfo.setRegisteredTime(fundAccount.getClientOprDt());
		userInfo.setTel(user.getTel());
		userInfo.setAddress(user.getAddress());
		userInfo.setStatus(DEFAULT_INUSE_STATUS);
		userInfo.setStatusChangeTime(fundAccount.getClientOprDt());
		//默认申请通过
		userInfo.setState(1);
		try {
			transferUserInfoDao.createTestObjcet(userInfo);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			LOGGER.info("错误的个人客户信息：{}", userInfo);
			throw new BusinessException("个人客户信息创建失败！");
		}
	}

	/**
	 * 创建单用客户信息
	 *
	 * @param fundAccount 资金账户信息
	 * @param user        构建的客户信息
	 */
	private void createCompanyInfo(FundAccount fundAccount, User user) {
		CompanyInfo company = new CompanyInfo();
		company.setId(user.getId());
		company.setUserName(user.getUserName());
		company.setUserIdType(user.getUserIdType());
		company.setUserIdNum(user.getUserIdNum());
		company.setDepartment(user.getDepartment());
		company.setRegisteredType(REGISTEREDTYPE_OFFLINE);
		company.setChannelId(user.getChannelId());
		company.setRegisteredTime(fundAccount.getClientOprDt());
		company.setTel(user.getTel());
		company.setAddress(user.getAddress());
		company.setAgentName(user.getAgentName());
		company.setAgentIdType(user.getAgentIdType());
		company.setAgentIdNum(user.getAgentIdNum());
		company.setStatus(DEFAULT_INUSE_STATUS);
		company.setStatusChangeTime(fundAccount.getClientOprDt());
		//默认申请通过
		company.setState(1);
		try {
			transferUserInfoDao.createTestObjcet(company);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			LOGGER.info("错误的单位客户信息：{}", company);
			throw new BusinessException("单位客户信息创建失败！");
		}
	}


}
